package com.example.notepad.Activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.notepad.Bean.Content;
import com.example.notepad.R;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class ContentActivity extends Activity {

    private EditText mTitle;
    private EditText mContent;
    private TextView upadteAt;
    private ImageView mBack;
    private ImageView mSave;
    private String userName;
    private String myId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        myId = getIntent().getStringExtra("id");
        userName = getIntent().getStringExtra("userName");
        moveStatusbar();
        initView();
        if(myId != null) {
            initData();
        }
        initonClick();
    }

    private void moveStatusbar() {
        if(Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    private void initView() {
        mTitle = (EditText) findViewById(R.id.note_title);
        mContent = (EditText) findViewById(R.id.note_content);
        upadteAt = (TextView) findViewById(R.id.note_date);
        mBack = (ImageView) findViewById(R.id.note_back);
        mSave = (ImageView) findViewById(R.id.note_save);
    }

    private void initData() {
        BmobQuery<Content> query = new BmobQuery<Content>();
        query.addWhereEqualTo("objectId",myId);
        query.findObjects(new FindListener<Content>() {
            @Override
            public void done(List<Content> list, BmobException e) {
                mTitle.setText(list.get(0).getTitle());
                mContent.setText(list.get(0).getContent());
                upadteAt.setText(list.get(0).getUpdatedAt());

            }
        });
    }

    private void initonClick() {
        mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String newTitle = mTitle.getText().toString();
                String newContent = mContent.getText().toString();
                if(newTitle.equals("") && newContent.equals("")) {
                    Toast.makeText(ContentActivity.this,"内容和标题不能同时为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                Content nContent = new Content();
                nContent.setTitle(newTitle);
                nContent.setContent(newContent);

                if(userName != null) {
                    nContent.setBelong(userName);
                    nContent.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e==null){
                                Toast.makeText(ContentActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(ContentActivity.this,"保存失败" + e.getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } else {
                    nContent.update(myId, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e==null){
                                Toast.makeText(ContentActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(ContentActivity.this,"保存失败" + e.getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                finish();
            }
        });
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(myId != null) {
            initData();
        }
    }

}